﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using qltscd.Helper;
using qltscd.Views.DanhMucThietBi.Models;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Text;
using System.Web.Routing;

namespace qltscd.Controllers
{
    public class DanhMucThietBiController : BaseController
    {
        protected override void OnActionExecuting (ActionExecutingContext filterContext) {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenDanhMuc] == '0') {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }
        #region Properties Section
        private QLTBEntities db = new QLTBEntities();
        //private DataClassesDataContext data = new DataClassesDataContext();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            //string tmp = (string)Session[SessionConstants.ERROR_MESSAGE];
            //ViewData[SessionConstants.ERROR_MESSAGE] = tmp;
            Session[SessionConstants.ERROR_MESSAGE] = string.Empty;
            DanhMucThietBiScreenClient screen = new DanhMucThietBiScreenClient();
            if (Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT] != null)
            {
                screen = (DanhMucThietBiScreenClient)Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT];
            }
            Search(screen);
            return View(screen);
        }
        [HttpPost]
        public ActionResult Index(DanhMucThietBiScreenClient screen)
        {
            Search(screen);
            return View(screen);
        }

        [GridAction]
        public ActionResult _Index()
        {
            DanhMucThietBiScreenClient screen = new DanhMucThietBiScreenClient();
            if (Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT] != null)
            {
                screen = (DanhMucThietBiScreenClient)Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT];
            }
            Search(screen);
            return View(new GridModel(screen.DanhMucThietBis));
        }

        public ActionResult Create () {
            Models.BaseClient.DanhMucThietBi thietbi = new Models.BaseClient.DanhMucThietBi() { IdentityThietBi = -1 ,NSX = DateTime.Now.Year, NCC = DateTime.Now.Year };
            string SerialThietBi = Request.QueryString["SerialThietBi"];
            if (SerialThietBi != null) {
                try {
                    DanhMucThietBi tb = db.DanhMucThietBis.Single(i => i.SerialThietBi == SerialThietBi);
                    if (tb != null) {
                        ObjectHelper.Copy(tb, thietbi);
                    }
                } catch (Exception) { }
                
            }
            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", thietbi.IdentityHopDong);
            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", thietbi.IdentityKieu);
            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", thietbi.IdentityLoaiTien);
            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", thietbi.IdentityNguoiDung);
            ViewBag.IdentityNguoiDung = TaoDanhSachNguoiDung(thietbi.IdentityNguoiDung);
            //ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", thietbi.IdentityNhaCungCap);
            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", thietbi.IdentityNuocSanXuat);
            ViewBag.MaThietBiCha = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi", thietbi.MaThietBiCha);
            return View(thietbi);
        }

        public ActionResult NhapTheoChungLoai () {
            string MaHopDong = Request.QueryString["MaHopDong"];
            DanhMucThietBiTheoHopDong screen = screen = (DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];
            if(screen == null )screen = new DanhMucThietBiTheoHopDong();
            screen.DSThietBi.Clear();
            if (MaHopDong != null) {
                DanhMucHopDong hopdong = db.DanhMucHopDongs.Single(i => i.MaHopDong == MaHopDong);
                if (hopdong != null) {
                    foreach (DanhMucThietBi thietbi in hopdong.DanhMucThietBis) {
                        Models.BaseClient.DanhMucThietBi thietbiClient = new Models.BaseClient.DanhMucThietBi();
                        ObjectHelper.Copy(thietbi, thietbiClient);
                        screen.DSThietBi.Add(thietbiClient);
                    }
                }
                screen.IdentityHopDong = hopdong.IdentityHopDong;

            }
            Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT] = screen;
            screen = SearchThietBiChungLoai(screen);
            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", screen.IdentityHopDong);
            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", screen.IdentityLoaiTien);
            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", screen.IdentityNhaCungCap);
            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", screen.IdentityLyDoTang);
            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", screen.IdentityHopDong);
            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", screen.IdentityKieu);
            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", screen.IdentityNhaCungCap);
            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", screen.IdentityNuocSanXuat);
            return View(screen);
        }

        [GridAction]
        public ActionResult _NhapTheoChungLoai () {
            DanhMucThietBiTheoHopDong screen = (DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];
            screen = SearchThietBiChungLoai(screen);
            return View(new GridModel(screen.DSThietBi));
        }

        public ActionResult ShowPdf()
        {
            ReportClass rpt = new ReportClass();
            rpt.FileName = Server.MapPath("~/") + "//Reports//SoTheoDoiQuanLyThietBi.rpt";
            rpt.SetDatabaseLogon("sa", "sa");
            rpt.Load();
            rpt.SetParameterValue("paraTenDonVi", qltscd.Helper.SessionHelper.DonViQuanLy);
            //rpt.SetDataSource(db.DanhMucThietBis);
            Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(stream, "application/pdf");
        }

        public FileResult ExportToExcel()
        {

            DataTable myTable = new DataTable();

            var data = from n in db.DanhMucThietBis
                       select n;

            myTable.Columns.Add("Mã Thiết Bị");
            myTable.Columns.Add("Tên Thiết Bị");
            myTable.Columns.Add("Thẻ Thiết Bị");
            myTable.Columns.Add("Serial Thiết Bị");
            myTable.Columns.Add("Năm Sản Xuất");
            myTable.Columns.Add("Năm Cung Cấp");
            myTable.Columns.Add("Nguyên Giá");
            myTable.Columns.Add("Tỉ Giá");
            myTable.Columns.Add("Thời Gian Khấu Hao");
            myTable.Columns.Add("Thời Diểm Khấu Hao");
            myTable.Columns.Add("Thời Gian Bảo Hành");
            myTable.Columns.Add("Thời Điểm Bảo Hành");
            myTable.Columns.Add("Chất Lượng");

            foreach (var n in data)
            {
                myTable.Rows.Add(n.MaThietBi.ToString(), n.TenThietBi.ToString(), n.TheThietBi, n.SerialThietBi, n.NamSanXuat, n.NamCungCap, n.NguyenGia, n.TiGia, n.ThoiGianKhauHao, n.ThoiDiemKhauHao, n.ThoiGianBaoHanh, n.ThoiDiemBaoHanh, n.ChatLuong);
            }

            Response.ContentType = "application/csv";
            Response.Charset = "";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + "BaoCaoDanhMucThietBi.xls");
            Response.ContentEncoding = Encoding.Unicode;
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());

            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("DANH MỤC THIẾT BỊ\t\t\t\n\n");
                for (int count = 0; count < myTable.Columns.Count; count++)
                {
                    if (myTable.Columns[count].ColumnName != null)
                        sb.Append(myTable.Columns[count].ColumnName);
                    if (count < myTable.Columns.Count - 1)
                        sb.Append("\t");
                }

                Response.Write(sb.ToString() + "\n");
                Response.Flush();
                int soDem = 0;
                while (myTable.Rows.Count >= soDem + 1)
                {
                    sb = new StringBuilder();
                    for (int col = 0; col < myTable.Columns.Count - 1; col++)
                    {
                        if (myTable.Rows[soDem][col] != null)
                            sb.Append(myTable.Rows[soDem][col].ToString().Replace(",", " "));
                        sb.Append("\t");
                    }
                    if (myTable.Rows[soDem][myTable.Columns.Count - 1] != null)
                        sb.Append(myTable.Rows[soDem][myTable.Columns.Count - 1].ToString().Replace(",", " "));
                    Response.Write(sb.ToString() + "\n");
                    Response.Flush();
                    soDem = soDem + 1;
                }
            }
            catch
            {

            }

            myTable.Dispose();
            Response.End();
            return File("BaoCaoDanhMucThietBi", "application/ms-excel", "BaoCaoDanhMucThietBi.xls");
        }

        public void CauHinhThietBi (int id) {
            using (ReportClass rpt = new ReportClass()) {
                rpt.FileName = Server.MapPath("~") + "\\Reports\\rChiTietCauHinhThietBi.rpt";
                rpt.SetDatabaseLogon("huynh", "1234567890");
                rpt.SetDataSource(db.DanhMucThietBis.ToList());
                rpt.Load();
                rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
            }
        }

        public ActionResult NhapTheoHopDong () {
            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong");
            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien");
            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap");
            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang");
            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu");
            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat");
            ViewBag.IdentityThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");

            DanhMucThietBiTheoHopDong screen = new DanhMucThietBiTheoHopDong();

            return View(screen);
        }
        [GridAction]
        public ActionResult _NhapTheoHopDong () {
            DanhMucThietBiTheoHopDong screen = (DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];
            screen = SearchThietBiHopDong(screen);
            return View(new GridModel(screen.DSThietBi));
        }

        public ActionResult ThanhLy () {
            Models.ScreenClient.ThanhLyScreenClient tl = new Models.ScreenClient.ThanhLyScreenClient { IdentityThanhLy = -1 };
            ViewBag.IdentityLyDoGiam = new SelectList(db.DanhMucLyDoGiams.OrderBy(i => i.TenLyDoGiam), "IdentityLyDoGiam", "TenLyDoGiam");
            ViewBag.MaThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "MaThietBi", "MaThietBi");
            ViewBag.SerialThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi");
            ViewBag.TenThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TenThietBi), "TenThietBi", "TenThietBi");
            ViewBag.TheThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TheThietBi), "TheThietBi", "TheThietBi");
            return View(tl);
        }
        [GridAction]
        public ActionResult _ThanhLy () {
            ThanhLyScreenClient thietbithanhly = SearchThietBiThanhLy();
            return View(new GridModel(thietbithanhly.DSThietBiThanhLy));
        }
        #endregion

        #region CRUD Section
        /// <summary>
        /// kieuLuu: 0 - luu, 1 - lu va nhap moi
        /// </summary>
        /// <param name="thietbi"></param>
        /// <param name="kieuLuu"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create (Models.BaseClient.DanhMucThietBi thietbi) {
            if (ModelState.IsValid) {
                bool valid = true;
                if (thietbi.IdentityThietBi == -1) { //nhap thiet bi moi
                    if (db.DanhMucThietBis.Count(i => i.TheThietBi == thietbi.TheThietBi) > 0) {
                        ViewData["errMsgTheThietBi"] = "Thẻ thiết bị đã tồn tại";
                        valid = false;
                    }
                    if (db.DanhMucThietBis.Count(i => i.SerialThietBi == thietbi.SerialThietBi) > 0) {
                        ViewData["errMsgSerialThietBi"] = "Serial thiết bị đã tồn tại";
                        valid = false;
                    }
                    if (db.DanhMucThietBis.Count(i => i.MaThietBi == thietbi.MaThietBi) > 0) {
                        ViewData["errMsgMaThietBi"] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                        valid = false;
                    }
                } else { //cap nhat thiet bi cu
                    DanhMucThietBi tb = db.DanhMucThietBis.Single(i => i.IdentityThietBi == thietbi.IdentityThietBi);
                    if (tb.TheThietBi != thietbi.TheThietBi) {
                        if (db.DanhMucThietBis.Count(i => i.TheThietBi == thietbi.TheThietBi) > 0) {
                            ViewData["errMsgTheThietBi"] = "Thẻ thiết bị đã tồn tại";
                            valid = false;
                        }
                    }
                    if (tb.SerialThietBi != thietbi.SerialThietBi) {
                        if (db.DanhMucThietBis.Count(i => i.SerialThietBi == thietbi.SerialThietBi) > 0) {
                            ViewData["errMsgSerialThietBi"] = "Serial thiết bị đã tồn tại";
                            valid = false;
                        }
                    }
                    if (tb.MaThietBi != thietbi.MaThietBi) {
                        if (db.DanhMucThietBis.Count(i => i.MaThietBi == thietbi.MaThietBi) > 0) {
                            ViewData["errMsgMaThietBi"] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                            valid = false;
                        }
                    }
                }
                if (valid) {
                    if (thietbi.NSX.HasValue) {
                        thietbi.NamSanXuat = new DateTime(thietbi.NSX.Value, 1, 1);
                    }
                    if (thietbi.NCC.HasValue) {
                        thietbi.NamCungCap = new DateTime(thietbi.NCC.Value, 1, 1);
                    }
                    if (thietbi.IdentityThietBi == -1) { //Create
                        if (db.DanhMucThietBis.Count(o => o.MaThietBi == thietbi.MaThietBi) > 0) {
                            Session[SessionConstants.ERROR_MESSAGE] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", thietbi.IdentityHopDong);
                            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", thietbi.IdentityKieu);
                            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", thietbi.IdentityLoaiTien);
                            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", thietbi.IdentityNguoiDung);
                            ViewBag.IdentityNguoiDung = TaoDanhSachNguoiDung(thietbi.IdentityNguoiDung);
                            //ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", thietbi.IdentityNhaCungCap);
                            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", thietbi.IdentityNuocSanXuat);
                            ViewBag.MaThietBiCha = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi", thietbi.MaThietBiCha);
                            return View(thietbi);
                        }
                        DanhMucThietBi tb = new DanhMucThietBi();
                        ObjectHelper.Copy(thietbi, tb);
                        db.DanhMucThietBis.AddObject(tb);
                    } else { //Update
                        DanhMucThietBi tb = db.DanhMucThietBis.Single(i => i.IdentityThietBi == thietbi.IdentityThietBi);                        
                        ObjectHelper.Copy(thietbi, tb);
                        db.ObjectStateManager.ChangeObjectState(tb, EntityState.Modified);
                    }
                    db.SaveChanges();
                    return RedirectToAction("Create");
                }
            }
            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", thietbi.IdentityHopDong);
            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", thietbi.IdentityKieu);
            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", thietbi.IdentityLoaiTien);
            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", thietbi.IdentityNguoiDung);
            ViewBag.IdentityNguoiDung = TaoDanhSachNguoiDung(thietbi.IdentityNguoiDung);
            //ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", thietbi.IdentityNhaCungCap);
            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", thietbi.IdentityNuocSanXuat);
            ViewBag.MaThietBiCha = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi", thietbi.MaThietBiCha);
            return View(thietbi);
        }

        [HttpPost]
        public ActionResult NhapTheoChungLoai (DanhMucThietBiTheoHopDong tbhd) {
            try {
                List<Models.BaseClient.DanhMucThietBi> thietbis = ((DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT]).DSThietBi;
                foreach (Models.BaseClient.DanhMucThietBi thietbi in thietbis) {
                    thietbi.IdentityHopDong = tbhd.IdentityHopDong;
                    thietbi.IdentityKieu = thietbi.IdentityKieu;
                    thietbi.IdentityNuocSanXuat = tbhd.IdentityNuocSanXuat;
                    thietbi.NamSanXuat = (tbhd.NSX.HasValue) ? new DateTime(tbhd.NSX.Value, 1, 1) : thietbi.NamSanXuat;
                    thietbi.NamCungCap = (tbhd.NCC.HasValue) ? new DateTime(tbhd.NCC.Value, 1, 1) : thietbi.NamCungCap;
                    thietbi.ThoiDiemBaoHanh = tbhd.ThoiDiemBaoHanh;
                    thietbi.ThoiGianKhauHao = tbhd.ThoiGianKhauHao;
                    thietbi.ThoiDiemKhauHao = tbhd.ThoiDiemKhauHao;
                    thietbi.ThoiGianBaoHanh = tbhd.ThoiGianBaoHanh;
                    thietbi.TiGia = tbhd.TiGia;
                    thietbi.NguyenGia = tbhd.NguyenGia;
                    thietbi.IdentityLoaiTien = tbhd.IdentityLoaiTien;
                    thietbi.IdentityLyDoTang = tbhd.IdentityLyDoTang; 
                    thietbi.IdentityNhaCungCap = tbhd.IdentityNhaCungCap;

                    if (thietbi.IdentityThietBi < 0) { // create new
                        if (db.DanhMucThietBis.Count(o => o.MaThietBi == thietbi.MaThietBi) > 0) {
                            ViewData[SessionConstants.ERROR_MESSAGE] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", tbhd.IdentityLoaiTien);
                            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", tbhd.IdentityLyDoTang);
                            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", tbhd.IdentityKieu);
                            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", tbhd.IdentityNuocSanXuat);
                            return View(tbhd);
                        }
                        DanhMucThietBi tb = new DanhMucThietBi();
                        ObjectHelper.Copy(thietbi, tb);
                        tb.IdentityThietBi = 0;
                        db.DanhMucThietBis.AddObject(tb);
                    } else { //update
                        DanhMucThietBi tb = db.DanhMucThietBis.Single(i => i.IdentityThietBi == thietbi.IdentityThietBi);
                        ObjectHelper.Copy(thietbi, tb);
                        db.ObjectStateManager.ChangeObjectState(tb, EntityState.Modified);
                    }
                }
                db.SaveChanges();
                return RedirectToAction("Index", "DanhMucHopDong");
            } catch {
                ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", tbhd.IdentityLoaiTien);
                ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", tbhd.IdentityLyDoTang);
                ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", tbhd.IdentityKieu);
                ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", tbhd.IdentityNuocSanXuat);
                return View(tbhd);
            }
        }

        [HttpPost]
        [GridAction]
        public ActionResult _UpdateDanhMucChungLoai ([Bind(Prefix = "inserted")]IEnumerable<Models.BaseClient.DanhMucThietBi> inserted,
            [Bind(Prefix = "updated")]IEnumerable<Models.BaseClient.DanhMucThietBi> updated,
            [Bind(Prefix = "deleted")]IEnumerable<Models.BaseClient.DanhMucThietBi> deleted) {
                Models.ScreenClient.DanhMucThietBiTheoHopDong hopdong = (Models.ScreenClient.DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];

            if (inserted != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in inserted) {
                    //tbClient.MaThietBi = KeyHelper.GetMaDanhMucThietBi();
                    tbClient.NamSanXuat = (tbClient.NSX.HasValue) ? new DateTime(tbClient.NSX.Value, 1, 1) : tbClient.NamSanXuat;
                    tbClient.NamCungCap = (tbClient.NCC.HasValue) ? new DateTime(tbClient.NCC.Value, 1, 1) : tbClient.NamCungCap;
                    tbClient.LyDoTang = (tbClient.IdentityLyDoTang.HasValue) ? db.DanhMucLyDoTangs.Single(i => i.IdentityLyDoTang == tbClient.IdentityLyDoTang).TenLyDoTang : "";
                    tbClient.IdentityThietBi = -hopdong.DSThietBi.Count - 1;
                    hopdong.DSThietBi.Add(tbClient);
                }
            }

            if (updated != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in updated) {
                    Models.BaseClient.DanhMucThietBi tb = hopdong.DSThietBi.Single(i => i.local_id == tbClient.local_id);
                    hopdong.DSThietBi.Remove(tb);
                    tbClient.NamSanXuat = (tbClient.NSX.HasValue) ? new DateTime(tbClient.NSX.Value, 1, 1) : tbClient.NamSanXuat;
                    tbClient.NamCungCap = (tbClient.NCC.HasValue) ? new DateTime(tbClient.NCC.Value, 1, 1) : tbClient.NamCungCap;
                    tbClient.LyDoTang = (tbClient.IdentityLyDoTang.HasValue) ? db.DanhMucLyDoTangs.Single(i => i.IdentityLyDoTang == tbClient.IdentityLyDoTang).TenLyDoTang : "";
                    ObjectHelper.Copy(tbClient, tb);
                    hopdong.DSThietBi.Add(tb);
                }
            }

            if (deleted != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in deleted) {
                    Models.BaseClient.DanhMucThietBi tb = hopdong.DSThietBi.Single(i => i.local_id == tbClient.local_id);
                    hopdong.DSThietBi.Remove(tb);
                }
            }
            Session[SessionScreenContansts.DanhMucHopDong_ThietBi_SCREENCLIENT] = hopdong;
            return View(new GridModel(hopdong.DSThietBi));
        }

        [HttpPost]
        [GridAction]
        public ActionResult _UpdateDanhMucHopDong ([Bind(Prefix = "inserted")]IEnumerable<Models.BaseClient.DanhMucThietBi> inserted,
            [Bind(Prefix = "updated")]IEnumerable<Models.BaseClient.DanhMucThietBi> updated,
            [Bind(Prefix = "deleted")]IEnumerable<Models.BaseClient.DanhMucThietBi> deleted) {
            Models.ScreenClient.DanhMucThietBiTheoHopDong hopdong = (Models.ScreenClient.DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];

            if (inserted != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in inserted) {
                    //tbClient.MaThietBi = KeyHelper.GetMaDanhMucThietBi();
                    tbClient.NamSanXuat = (tbClient.NSX.HasValue) ? new DateTime(tbClient.NSX.Value, 1, 1) : tbClient.NamSanXuat;
                    tbClient.NamCungCap = (tbClient.NCC.HasValue) ? new DateTime(tbClient.NCC.Value, 1, 1) : tbClient.NamCungCap;
                    tbClient.LyDoTang = (tbClient.IdentityLyDoTang.HasValue) ? db.DanhMucLyDoTangs.Single(i => i.IdentityLyDoTang == tbClient.IdentityLyDoTang).TenLyDoTang : "";
                    tbClient.IdentityThietBi = -hopdong.DSThietBi.Count - 1;
                    hopdong.DSThietBi.Add(tbClient);
                }
            }

            if (updated != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in updated) {
                    Models.BaseClient.DanhMucThietBi tb = hopdong.DSThietBi.Single(i => i.local_id == tbClient.local_id);
                    hopdong.DSThietBi.Remove(tb);
                    tbClient.NamSanXuat = (tbClient.NSX.HasValue) ? new DateTime(tbClient.NSX.Value, 1, 1) : tbClient.NamSanXuat;
                    tbClient.NamCungCap = (tbClient.NCC.HasValue) ? new DateTime(tbClient.NCC.Value, 1, 1) : tbClient.NamCungCap;
                    tbClient.LyDoTang = (tbClient.IdentityLyDoTang.HasValue) ? db.DanhMucLyDoTangs.Single(i => i.IdentityLyDoTang == tbClient.IdentityLyDoTang).TenLyDoTang : "";
                    ObjectHelper.Copy(tbClient, tb);
                    hopdong.DSThietBi.Add(tb);
                }
            }

            if (deleted != null) {
                foreach (Models.BaseClient.DanhMucThietBi tbClient in deleted) {
                    Models.BaseClient.DanhMucThietBi tb = hopdong.DSThietBi.Single(i => i.local_id == tbClient.local_id);
                    hopdong.DSThietBi.Remove(tb);
                }
            }
            Session[SessionScreenContansts.DanhMucHopDong_ThietBi_SCREENCLIENT] = hopdong;
            return View(new GridModel(hopdong.DSThietBi));
        }


        public ActionResult Delete (int id) {
            DanhMucThietBi thietbi = db.DanhMucThietBis.Single(i => i.IdentityThietBi == id);
            db.DanhMucThietBis.DeleteObject(thietbi);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult ThanhLy (qltscd.Models.ScreenClient.ThanhLyScreenClient tbhd) {
            if (ModelState.IsValid) {

                qltscd.Models.ThanhLy tl = new qltscd.Models.ThanhLy();


                if (tbhd.IdentityThanhLy == -1) {
                    if (db.ThanhLies.Count(o => o.MaThanhLy == tbhd.MaThanhLy) > 0) {
                        ViewData[SessionConstants.ERROR_MESSAGE] = "   Mã Thanh lý " + tbhd.MaThanhLy + " đã tồn tại";
                        return View(tbhd);
                    }
                    ObjectHelper.Copy(tbhd, tl);
                    db.ThanhLies.AddObject(tl);
                    try {
                        List<Models.BaseClient.DanhMucThietBi> thietbis = ((ThanhLyScreenClient)Session[SessionScreenContansts.ThanhLy_SCREENCLIENT]).DSThietBiThanhLy;
                        foreach (Models.BaseClient.DanhMucThietBi thietbi in thietbis) {
                            thietbi.IdentityLyDoGiam = tbhd.IdentityLyDoGiam;
                            thietbi.IdentityThanhLy = tbhd.IdentityThanhLy;


                            // return RedirectToAction("Index");
                            //}
                            // ViewBag.IdentityChungLoai = new SelectList(db.DanhMucChungLoais, "IdentityChungLoai", "TenChungLoai", kC.IdentityChungLoai);
                            //ViewBag.IdentityHangSanXuat = new SelectList(db.DanhMucHangSanXuats, "IdentityHangSanXuat", "TenHangSanXuat", kC.IdentityHangSanXuat);
                            // return View(tbhd);

                            if (db.DanhMucThietBis.Count(o => o.MaThietBi == thietbi.MaThietBi) > 0) {
                                Session[SessionConstants.ERROR_MESSAGE] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                                return View(tbhd);
                            }


                            DanhMucThietBi tb = new DanhMucThietBi();
                            ObjectHelper.Copy(thietbi, tb);
                            //DanhMucThietBi ki = db.DanhMucThietBis.Single(i => i.IdentityThietBi == tbhd.IdentityThietBi);
                            //ObjectHelper.Copy(tbhd, ki);
                            //db.ObjectStateManager.ChangeObjectState(ki, EntityState.Modified);
                            //db.SaveChanges();

                            if (db.DanhMucThietBis.Count(o => o.IdentityThietBi == thietbi.IdentityThietBi) == 0) {
                                db.DanhMucThietBis.AddObject(tb);
                            } else {
                                db.ObjectStateManager.ChangeObjectState(thietbi, EntityState.Modified);
                            }
                        }
                        db.SaveChanges();
                        return RedirectToAction("ThanhLy");
                    } catch {

                        ViewBag.IdentityLyDoGiam = new SelectList(db.DanhMucLyDoGiams.OrderBy(i => i.IdentityLyDoGiam), "IdentityLyDoGiam", "TenLyDoGiam", tbhd.IdentityLyDoGiam);
                        //  ViewBag.IdentityThanhLy = new SelectList(db.ThanhLies.OrderBy(i => i.NguoiKy), "IdentityThanhLy", "NguoiKy", tbhd.IdentityThanhLy);
                        ViewBag.MaThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "MaThietBi", "MaThietBi");
                        ViewBag.SerialThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi");
                        ViewBag.TenThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TenThietBi), "TenThietBi", "TenThietBi");
                        ViewBag.TheThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TheThietBi), "TheThietBi", "TheThietBi");
                        // return View(new GridModel(qltscd.Contansts.SessionThietBi.All()));
                        return View(tbhd);
                    }
                } else {
                    tl = db.ThanhLies.Single(i => i.IdentityThanhLy == tbhd.IdentityThanhLy);//lấy 1 thằng
                    if (tbhd.MaThanhLy != tl.MaThanhLy) {
                        if (db.ThanhLies.Count(o => o.MaThanhLy == tbhd.MaThanhLy) > 0) {
                            ViewData[SessionConstants.ERROR_MESSAGE] = "   Mã Thanh lý " + tbhd.MaThanhLy + " đã tồn tại";
                            return View(tbhd);
                        }
                    }
                    ObjectHelper.Copy(tbhd, tl);
                    db.ObjectStateManager.ChangeObjectState(tl, EntityState.Modified);


                    //  db.SaveChanges();
                    //return RedirectToAction("ThanhLy");
                    //     }
                    try {
                        List<Models.BaseClient.DanhMucThietBi> thietbis = ((ThanhLyScreenClient)Session[SessionScreenContansts.ThanhLy_SCREENCLIENT]).DSThietBiThanhLy;
                        foreach (Models.BaseClient.DanhMucThietBi thietbi in thietbis) {
                            thietbi.IdentityLyDoGiam = tbhd.IdentityLyDoGiam;
                            thietbi.IdentityThanhLy = tbhd.IdentityThanhLy;

                            if (db.DanhMucThietBis.Count(o => o.MaThietBi == thietbi.MaThietBi) > 0) {
                                Session[SessionConstants.ERROR_MESSAGE] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                                return View(tbhd);
                            }


                            DanhMucThietBi tb = new DanhMucThietBi();
                            ObjectHelper.Copy(thietbi, tb);
                            //DanhMucThietBi ki = db.DanhMucThietBis.Single(i => i.IdentityThietBi == tbhd.IdentityThietBi);
                            //ObjectHelper.Copy(tbhd, ki);
                            //db.ObjectStateManager.ChangeObjectState(ki, EntityState.Modified);
                            //db.SaveChanges();

                            if (db.DanhMucThietBis.Count(o => o.IdentityThietBi == thietbi.IdentityThietBi) == 0) {
                                db.DanhMucThietBis.AddObject(tb);
                            } else {
                                db.ObjectStateManager.ChangeObjectState(thietbi, EntityState.Modified);
                            }
                        }
                        db.SaveChanges();
                        return RedirectToAction("ThanhLy");
                    } catch {

                        ViewBag.IdentityLyDoGiam = new SelectList(db.DanhMucLyDoGiams.OrderBy(i => i.IdentityLyDoGiam), "IdentityLyDoGiam", "TenLyDoGiam", tbhd.IdentityLyDoGiam);
                        //  ViewBag.IdentityThanhLy = new SelectList(db.ThanhLies.OrderBy(i => i.NguoiKy), "IdentityThanhLy", "NguoiKy", tbhd.IdentityThanhLy);
                        ViewBag.MaThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "MaThietBi", "MaThietBi");
                        ViewBag.SerialThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.IdentityThietBi), "IdentityThietBi", "SerialThietBi");
                        ViewBag.TenThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TenThietBi), "TenThietBi", "TenThietBi");
                        ViewBag.TheThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.TheThietBi), "TheThietBi", "TheThietBi");
                        // return View(new GridModel(qltscd.Contansts.SessionThietBi.All()));
                        return View(tbhd);
                    }
                }
                //  db.SaveChanges();
                //  return RedirectToAction("ThanhLy");
            }
            db.SaveChanges();
            return RedirectToAction("ThanhLy");
        }

        [HttpPost]
        public ActionResult NhapTheoHopDong (DanhMucThietBiTheoHopDong tbhd) {
            try {
                List<Models.BaseClient.DanhMucThietBi> thietbis = ((DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT]).DSThietBi;
                foreach (Models.BaseClient.DanhMucThietBi thietbi in thietbis) {
                    thietbi.IdentityHopDong = tbhd.IdentityHopDong;
                    thietbi.IdentityLoaiTien = tbhd.IdentityLoaiTien;
                    thietbi.IdentityNhaCungCap = tbhd.IdentityNhaCungCap;
                    thietbi.TiGia = tbhd.TiGia;
                    thietbi.ThoiDiemKhauHao = tbhd.ThoiDiemKhauHao;
                    thietbi.ThoiDiemBaoHanh = tbhd.ThoiDiemBaoHanh;
                    thietbi.NamSanXuat = (thietbi.NSX.HasValue) ? new DateTime(thietbi.NSX.Value, 1, 1) : thietbi.NamSanXuat;
                    thietbi.NamCungCap = (thietbi.NCC.HasValue) ? new DateTime(thietbi.NCC.Value, 1, 1) : thietbi.NamCungCap;
                    thietbi.IdentityLyDoTang = tbhd.IdentityLyDoTang;
                    if (thietbi.IdentityThietBi < 0) { // create new
                        if (db.DanhMucThietBis.Count(o => o.MaThietBi == thietbi.MaThietBi) > 0) {
                            ViewData[SessionConstants.ERROR_MESSAGE] = "Mã thiết bị " + thietbi.MaThietBi + " đã tồn tại";
                            ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                            ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", tbhd.IdentityLoaiTien);
                            ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", tbhd.IdentityLyDoTang);
                            ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", tbhd.IdentityKieu);
                            ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                            ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", tbhd.IdentityNuocSanXuat);
                            return View(tbhd);
                        }
                        DanhMucThietBi tb = new DanhMucThietBi();
                        ObjectHelper.Copy(thietbi, tb);
                        tb.IdentityThietBi = 0;
                        db.DanhMucThietBis.AddObject(tb);
                    } else { //update
                        DanhMucThietBi tb = db.DanhMucThietBis.Single(i => i.IdentityThietBi == thietbi.IdentityThietBi);
                        ObjectHelper.Copy(thietbi, tb);
                        db.ObjectStateManager.ChangeObjectState(tb, EntityState.Modified);
                    }
                }
                db.SaveChanges();
                return RedirectToAction("Index", "DanhMucHopDong");
            } catch {
                ViewBag.IdentityHopDong = new SelectList(db.DanhMucHopDongs.OrderBy(i => i.SoHopDong), "IdentityHopDong", "SoHopDong", tbhd.IdentityHopDong);
                ViewBag.IdentityLoaiTien = new SelectList(db.DanhMucLoaiTiens.OrderBy(i => i.TenLoaiTien), "IdentityLoaiTien", "TenLoaiTien", tbhd.IdentityLoaiTien);
                ViewBag.IdentityLyDoTang = new SelectList(db.DanhMucLyDoTangs.OrderBy(i => i.TenLyDoTang), "IdentityLyDoTang", "TenLyDoTang", tbhd.IdentityLyDoTang);
                ViewBag.IdentityKieu = new SelectList(db.DanhMucKieux.OrderBy(i => i.TenKieu), "IdentityKieu", "TenKieu", tbhd.IdentityKieu);
                ViewBag.IdentityNhaCungCap = new SelectList(db.DanhMucNhaCungCaps.OrderBy(i => i.TenNhaCungCap), "IdentityNhaCungCap", "TenNhaCungCap", tbhd.IdentityNhaCungCap);
                ViewBag.IdentityNuocSanXuat = new SelectList(db.DanhMucNuocSanXuats.OrderBy(i => i.TenNuocSanXuat), "IdentityNuocSanXuat", "TenNuocSanXuat", tbhd.IdentityNuocSanXuat);
                ViewBag.IdentityThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.MaThietBi), "IdentityThietBi", "SerialThietBi");
                return View(tbhd);
            }
        }
        #endregion

        #region Helper Section
        private ThanhLyScreenClient SearchThietBiThanhLy () {
            ThanhLyScreenClient tbhd = new ThanhLyScreenClient() { IdentityThanhLy = -1 };
            if (Session[SessionScreenContansts.ThanhLy_SCREENCLIENT] != null) {
                tbhd = (ThanhLyScreenClient)Session[SessionScreenContansts.ThanhLy_SCREENCLIENT];
            }
            Session[SessionScreenContansts.ThanhLy_SCREENCLIENT] = tbhd;
            return tbhd;
        }

        private void Search(DanhMucThietBiScreenClient screen)
        {
            if (Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT] != null)
            {
                screen = (DanhMucThietBiScreenClient)Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT];
            }
            else
            {
                screen = new DanhMucThietBiScreenClient();
            }
            screen.DanhMucThietBis.Clear();
            var list = db.DanhMucThietBis.OrderBy(i => i.TheThietBi).ToList();
            foreach (DanhMucThietBi DMTB in list)
            {
                qltscd.Models.BaseClient.DanhMucThietBi DMTBClient = new Models.BaseClient.DanhMucThietBi();
                ObjectHelper.Copy(DMTB, DMTBClient);
                DMTBClient.NSX = (DMTBClient.NamSanXuat.HasValue) ? DMTBClient.NamSanXuat.Value.Year : DMTBClient.NSX;
                DMTBClient.NCC = (DMTBClient.NamCungCap.HasValue) ? DMTBClient.NamCungCap.Value.Year : DMTBClient.NCC;
                DMTBClient.LyDoTang = (DMTB.IdentityLyDoTang.HasValue) ? DMTB.DanhMucLyDoTang.TenLyDoTang : "";
                screen.DanhMucThietBis.Add(DMTBClient);
            }
            Session[SessionScreenContansts.DanhMucThietBi_SCREENCLIENT] = screen;
        }

        private SelectList TaoDanhSachNguoiDung (int? selected) {
            List<DanhMucNguoiDung> List = db.DanhMucNguoiDungs.OrderBy(i => i.TenNguoiDung).ToList();
            List<SelectableNguoiDung> nds = new List<SelectableNguoiDung>();
            foreach (DanhMucNguoiDung nd in List) {
                nds.Add(new SelectableNguoiDung(nd.IdentityNguoiDung, nd.HoTenDem, nd.TenNguoiDung));
            }
            return new SelectList(nds, "IdentityNguoiDung", "HoTenNguoiDung", selected);
        }

        private DanhMucThietBiTheoHopDong SearchThietBiChungLoai (DanhMucThietBiTheoHopDong screen) {
            if (screen == null)
                screen = new DanhMucThietBiTheoHopDong();
            if (Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT] != null) {
                screen = (DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];
            }
            Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT] = screen;
            return screen;
        }

        private DanhMucThietBiTheoHopDong SearchThietBiHopDong (DanhMucThietBiTheoHopDong screen) {
            if (screen == null)
                screen = new DanhMucThietBiTheoHopDong();
            if (Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT] != null) {
                screen = (DanhMucThietBiTheoHopDong)Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT];
            }
            foreach (Models.BaseClient.DanhMucThietBi thietbi in screen.DSThietBi) {
                thietbi.TenThietBiCha = (thietbi.MaThietBiCha.HasValue) ? db.DanhMucThietBis.Single(i => i.IdentityThietBi == thietbi.MaThietBiCha).SerialThietBi : "";
            }
            Session[SessionScreenContansts.DanhMucThietBiTheoHopDong_SCREENCLIENT] = screen;
            return screen;
        }
        #endregion

        #region Sub Section
        public ActionResult GetTenNhaCungCap () {
            string IdentityHopDong = Request.QueryString["hopdong"];
            int hopdongId = Convert.ToInt32(IdentityHopDong);
            DanhMucHopDong hopdong = db.DanhMucHopDongs.Single(i => i.IdentityHopDong == hopdongId);
            string TenNhaCungCap = (hopdong != null) ? hopdong.DanhMucNhaCungCap.TenNhaCungCap : "";
            return this.Json(new { TenNhaCungCap = TenNhaCungCap }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Additional Section
        #endregion
    }
}